package com.itboye.textileexpres.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OrderDeltilteBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActvityOrderDetail extends BaseActivity implements
		OnClickListener, Observer {
	TextView title;
	ImageView back, img_other;
	String type;
	MyOrderPresenter orderPresenter;
	OrderDeltilteBeans beans;
	String order_code;
	private TextView order_num;
	private TextView goods_detail_tv;
	private TextView color_tv, price_tv, num_tv, create_time;
	String addre;
	private TextView tvshouhuo, textPhone, textPadd, faburen, cole, tvyunfei,
			tvprice, tvcandi;
	private ImageView goods_img;

	private TextView btnDelete;
	private TextView dengdaipingjia;
	private LinearLayout litouytDetait;

	// 交易关闭
	private LinearLayout linearguanbi;
	// 快递
	private RelativeLayout linearkuaidi;
	// 时间
	private RelativeLayout lineyoutstar;
	// 传过来的
	private String str;
	private TextView shengyutime;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_order_detail;
	}

	@Override
	public void onMyCreate() {
		title.setText("订单详情");
		img_other.setVisibility(View.GONE);
		// TODO Auto-generated method stub

		order_code = getIntent().getStringExtra("order_code");
		System.out.println("order_code_code" + order_code);
		str = getIntent().getStringExtra("str");
		System.out.println("strstr" + str);
		beans = new OrderDeltilteBeans();
		type = getIntent().getStringExtra("type");
		// onSstartStr();
		orderPresenter = new MyOrderPresenter(this);
		System.out.println("notenotea" + beans.getNote());
		orderPresenter.getorder("39", order_code);
		shengyutime.setText(TimeFormat.onTime(beans));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == orderPresenter.Xiang_success) {
				beans = (OrderDeltilteBeans) entity.getData();
				System.out.println("notenote"
						+ beans.getItems().get(0).getName());
				getCounte(beans);
				onAddres(beans);
			} else if (entity.getEventType() == orderPresenter.Delete_success) {
				toast(entity.getData() + "");

			}
		}
	}

	public void onSstartStr() {

		if (str.equals("1")) {
			// toast("等待买家付款");
			cole.setText("等待买家付款");
			dengdaipingjia.setText("等待买家付款");

		} else if (str.equals("2")) {
			// toast("卖家已发货");
			cole.setText("卖家已发货");
			dengdaipingjia.setText("卖家已发货");

		} else if (str.equals("3")) {
			// toast("等待买家收货");
			cole.setText("等待返货");
			dengdaipingjia.setText("等待发货");
		} else if (str.equals("4")) {
			// toast("买家收货");
			linearguanbi.setVisibility(View.VISIBLE);
			linearkuaidi.setVisibility(View.VISIBLE);
			lineyoutstar.setVisibility(View.GONE);
			cole.setText("买家已收货");
			dengdaipingjia.setText("买家已收货");
		} else if (str.equals("7")) {
			linearguanbi.setVisibility(View.VISIBLE);
			linearkuaidi.setVisibility(View.GONE);
			lineyoutstar.setVisibility(View.GONE);
			cole.setText("交易成功");
			dengdaipingjia.setText("交易成功");
			// toast("交易成功");
		} else if (str.equals("8")) {
			cole.setText("交易关闭");
			dengdaipingjia.setText("交易关闭");
		}

	}

	private void onAddres(OrderDeltilteBeans beans) {
		if (beans.get_address() == null) {
			return;
		} else {
			addre = beans.get_address().getCountry()
					+ beans.get_address().getProvince()
					+ beans.get_address().getDetailinfo();
			tvshouhuo.setText(beans.getContactname());
			textPhone.setText(beans.get_address().getMobile());
			textPadd.setText(addre);

			System.out.println("time" + beans.get_auto_op_time());
			shengyutime.setText(beans.get_auto_op_time());
			// tvcandi.setText("产地:"+beans.getContactname());
			faburen.setText("发布人:" + beans.getPublisher_name());

			order_num.setText(order_code);
		}
	}

	private void getCounte(OrderDeltilteBeans beans) {
		System.out.println("LLLLLLLLLLLLLLLLL" + beans.getItems().size());
		for (int i = 0; i < beans.getItems().size(); i++) {

			if (beans.getContactname() == null) {
				toast("数据为空");
			} else {
				// onSstartStr();
				toast("" + beans.getNote());
				System.out.println("beansbeans" + beans.getItems().size());
				View view = LayoutInflater.from(ActvityOrderDetail.this)
						.inflate(R.layout.item_order_detail, null);
				TextView goods_name1_tv = (TextView) view
						.findViewById(R.id.goods_name1_tv);
				TextView goods_name2_tv = (TextView) view
						.findViewById(R.id.goods_name2_tv);
				TextView goods_detail_tv = (TextView) view
						.findViewById(R.id.goods_detail_tv);
				TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
				TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
				TextView num_tv = (TextView) view.findViewById(R.id.num_tv);

				goods_name1_tv.setText(beans.getPublisher_name());
				goods_name2_tv.setText(beans.getItems().get(i).getName());
				goods_detail_tv.setText(beans.getPublisher_name());
				color_tv.setText(beans.getItems().get(i).getSku_desc());
				price_tv.setText(beans.getItems().get(i).getPrice());
				num_tv.setText(beans.getItems().get(i).getCount());

				ImageView goods_img = (ImageView) view
						.findViewById(R.id.goods_img);
				create_time
						.setText(TimeFormat.DateFormat(beans.getCreatetime()));
				tvyunfei.setText(beans.getItems().get(i).getOri_price() + "元");
				tvprice.setText(beans.getItems().get(i).getDt_origin_country());
				XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
						+ beans.getItems().get(i).getImg(), goods_img);
				MarginLayoutParams layoutParams = new MarginLayoutParams(
						MarginLayoutParams.WRAP_CONTENT,
						MarginLayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(5, 5, 5, 5);
				litouytDetait.addView(view, layoutParams);

			}
		}
	}

}
